package com.shire.shire_interface.controller.clientversion;

import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.common.utils.StrUtils;
import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.model.clientversion.ClientVersion;
import com.shire.shire_interface.model.clientversion.ClientVersionExample;
import com.shire.shire_interface.service.clientversion.ClientVersionService;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;

@RestController
@RequestMapping("/clientver")
public class ApiClientVersionController {

    @Resource
    private ClientVersionService clientVersionService;

    @RequestMapping("/list")
    public Response getClientVersionList(){
        Response response = new Response();

        try{
            ClientVersionExample example = new ClientVersionExample();
            example.setOrderByClause("id desc");

            response.setData(clientVersionService.getByExample(example));
            response.setMessage("成功");
        } catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }
    @RequestMapping(value = "/uploadVersion", method = RequestMethod.POST)
    public Response onUpload( @RequestParam(value = "file") MultipartFile file) {
        Response response = new Response();
        try {
            ClientVersion clientVersion = new ClientVersion();
            clientVersion.setStatus(Constants.CommonStatus.ENABLE.ordinal());
            String oriName = file.getOriginalFilename();
            clientVersion.setOriname(oriName);
            clientVersion.setFilesize((int) file.getSize());
            // 设置版本 jerryinfo-v100.deb
            int lastDot = oriName.lastIndexOf(".");
            int lastSplit = oriName.lastIndexOf("-");
            if (lastSplit < 0 || lastSplit >= lastDot - 2) {
                clientVersion.setVersion(-1);
            } else {
                String verStr = oriName.substring(lastSplit + 2, lastDot);
                clientVersion.setVersion(StrUtils.getIntValue(verStr, -2));
            }

            File path = new File(ResourceUtils.getURL("classpath:").getPath());
            if (!path.exists()) {
                path = new File("");
            }

            String targetDir = "clientversion";

            String dirPath = "static" + File.separator + targetDir + File.separator;
            File upload = new File(path.getAbsolutePath(), dirPath);
            if (!upload.exists()) {
                boolean mkdirs = upload.mkdirs();
            }
            String newFileName = System.currentTimeMillis() + "-" + oriName;
            File uploadFile = new File(path.getAbsolutePath(), dirPath + newFileName);

            org.apache.commons.io.FileUtils.copyInputStreamToFile(file.getInputStream(), uploadFile);

            clientVersion.setFilepath(targetDir + File.separator + newFileName);
            clientVersion.setCreatetime(new Date());
            clientVersion.setUpdatetime(new Date());
            clientVersionService.insertClientVersion(clientVersion);
            response.success("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        return response;
    }
}
